package geon.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import geon.model.Aluno;
import geon.model.Email;
import geon.model.Endereco;
import geon.model.Site;
import geon.model.Telefone;

public class DaoAluno implements Dao<Aluno> {
	
	private DaoGenerico dao;
	private String sql;

	public DaoAluno() {
		dao = new DaoGenerico();
	}

	@Override
	public Integer add(Aluno o) throws SQLException {
		try {
			dao.begin();
			sql = "Insert Into TbPessoa (PesCodigo, PesNome) Values (?, ?)";
			o.setCodigo(dao.getNextId());
			dao.executeCommand(sql, o.getCodigo(), o.getNome());
			
			DaoNascimento nascimento = new DaoNascimento();
			nascimento.add(o.getNascimento(), o.getCodigo());
			
			sql = "Insert Into TbPessoaFisica (PesCodigo, PsfCpf, PsfRg, PsfDataExpedicao, PsfOrgaoExpedidor) Values (?, ?, ?, ?, ?)";
			dao.executeCommand(sql, o.getCodigo(), o.getCpf(), o.getRg(), o.getDataExpedicao(), o.getOrgaoExpedidor());
			
			sql = "Insert Into TbPessoaEndereco (PesCodigo, EndCodigo) Values (?, ?)";
			for (Endereco end : o.getEnderecos()) {
				dao.executeCommand(sql, o.getCodigo(), end.getCodigo());
			}			
			sql = "Insert Into TbPessoaTelefone (PesCodigo, TelCodigo) Values (?, ?)";			
			for (Telefone tel : o.getTelefones()) {
				dao.executeCommand(sql, o.getCodigo(), tel.getCodigo());
			}			
			sql = "Insert Into TbPessoaEmail (PesCodigo, EmaCodigo) Values (?, ?)";			
			for (Email ema : o.getEmails()) {
				dao.executeCommand(sql, o.getCodigo(), ema.getCodigo());
			}			
			sql = "Insert Into TbPessoaSite (PesCodigo, SitCodigo) Values (?, ?)";			
			for (Site sit : o.getSites()) {
				dao.executeCommand(sql, o.getCodigo(), sit.getCodigo());
			}			
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
		return o.getCodigo();
	}

	@Override
	public void upd(Aluno o) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void del(int codigo) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Aluno set(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Aluno get(int codigo) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Aluno get(Object param) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Aluno> list() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Aluno> list(Object... params) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
